﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;


[AddComponentMenu("Tween/Tween Rotation Axi")]
public class TweenRotationAxis : UITweener
{
    public enum Axis
    {
        Axis_X = 0,
        Axis_Y = 1,
        Axis_Z = 2,

    }

    public Axis axis = Axis.Axis_X;

    public float from;
    public float to;

    //public Vector3 from;
    //public Vector3 to;
    public bool quaternionLerp = false;

    Transform mTrans;

    public Transform cachedTransform { get { if (mTrans == null) mTrans = transform; return mTrans; } }

    /// <summary>
    /// Tween's current value.
    /// </summary>

    //public Quaternion value { get { return cachedTransform.localRotation; } set { cachedTransform.localRotation = value; } }

    public float value
    {
        get
        {
            switch (axis)
            {
                case Axis.Axis_X:
                    return cachedTransform.localRotation.x;
                case Axis.Axis_Y:
                    return cachedTransform.localRotation.y;
                case Axis.Axis_Z:
                    return cachedTransform.localRotation.z;
            }
            return cachedTransform.localPosition.x;
        }
        set
        {
            switch (axis)
            {
                case Axis.Axis_X:
                    cachedTransform.localRotation = Quaternion.Euler( new Vector3( value, cachedTransform.localEulerAngles.y, cachedTransform.localEulerAngles.z) );
                    break;
                case Axis.Axis_Y:
                    cachedTransform.localRotation = Quaternion.Euler(new Vector3(cachedTransform.localEulerAngles.x,value, cachedTransform.localEulerAngles.z));
                    break;
                case Axis.Axis_Z:
                    cachedTransform.localRotation = Quaternion.Euler(new Vector3(cachedTransform.localEulerAngles.x, cachedTransform.localEulerAngles.y , value));
                    break;
            }
        }
    }

    /// <summary>
    /// Tween the value.
    /// </summary>

    protected override void OnUpdate(float factor, bool isFinished){    value = from * (1f - factor) + to * factor;}

    /// <summary>
    /// Start the tweening operation.
    /// </summary>

    static public TweenRotation Begin(GameObject go, float duration, Quaternion rot)
    {
        TweenRotation comp = UITweener.Begin<TweenRotation>(go, duration);
        comp.from = comp.value.eulerAngles;
        comp.to = rot.eulerAngles;

        if (duration <= 0f)
        {
            comp.Sample(1f, true);
            comp.enabled = false;
        }
        return comp;
    }

    [ContextMenu("Set 'From' to current value")]
    public override void SetStartToCurrentValue() {  from = value; }

    [ContextMenu("Set 'To' to current value")]
    public override void SetEndToCurrentValue() { to = value; }

    [ContextMenu("Assume value of 'From'")]
    void SetCurrentValueToStart() { value = from; }

    [ContextMenu("Assume value of 'To'")]
    void SetCurrentValueToEnd() { value = to; }

}
